define(["jquery","bbcare","comm/datatable/datatable","comm/form/form","bch/app/classManager/classDetail.servers","bch/app/classManager/classDetail.ui","bch/app/classManager/classDetail.event"],function($,$bb,$datatable,$form,$servers,$ui,$event){
	var classDetail = {};
	var URL = $bb.baseUrl("followapp");

	/**
	 * 建档成功后的回调函数
	 */
	var ajaxArchiveCallBack = function(data,className){
		$("." + className).modal("hide");
		$bb.toastShow("添加成功！");
		$datatable.table.api().ajax.reload();  // 出发dt的重新加载数据的方法
        var args = $datatable.table.api().ajax.params();   //获取dt请求参数
	}

	/**
	 * 建档保存
	 */
	var onArchiveSave = function(){
		var params = $(this).data("params");
		if( $form.validate(params.domId) ){
			var doctorName = JSON.parse($bb.getSessionStorage("account")).alias;
			var channelId = $bb.getChannelId();
			var contList = $form.getSaveData(params.domId);
			var userName = $bb.code.getValueByName(params.domId,"userName");
			var taskIdList = $servers.getTaskIdList(params.questions);
			var params1 = {
				"oprInfo":{
					"opCode":"1000",
					"opType":"2",
					"opNote":"【"+doctorName+"】医生添加【"+userName+"】会员",
					"channelType":"2", // web端门诊渠道:2
					"channelId":channelId,
				},
				"archiveInfo":{
					"queueId":params.queueId,
					"schemeId":params.schemeId,
					"contList":contList,
					"taskIdList":taskIdList,
				},
			};
			$servers.ajaxArchive(params1,ajaxArchiveCallBack,params.className);
		}
	}

	/**
	 * 建档模板查询成功后的回调函数
	 */
	var ajaxArchiveTaskAndContCallBack = function(data,params){
		if( !params.questions ){
			if( data.taskList && data.taskList.length > 0 ){
				params.questions = data.taskList;
			}else if( data.contList && data.contList.length > 0 ){
				params.questions = data.contList;
			}
		}
		$.extend(params,{"domId":"classDetailModalContent"});
		$form.init({
			"id":params.domId,
			"questions":params.questions,
			"values":data.valueList,
			"event":{
				"autoCompleteCallBack":ajaxArchiveTaskAndContCallBack,
				"autoCompleteParams":params
			}
		});
		$("." + params.className).modal("show");
		// 建档保存
		$("." + params.className + " .classDetail-submit").unbind("click");
		$("." + params.className + " .classDetail-submit").data("params",params);
		$("." + params.className + " .classDetail-submit").click(onArchiveSave);
		// 取消
		$("." + params.className + " .classDetail-cancel").unbind("click");
		$("." + params.className + " .classDetail-cancel").data("className",params.className);
		$("." + params.className + " .classDetail-cancel").click($event.onCancel);
	}

	/**
	 * 队列下临床路径查询
	 */
	var ajaxQryQueueCpCallBack = function(data,params){
		if( data.rows.length > 0 ){
			$.extend(params,{"schemeId":data.rows[0].id});
			$servers.ajaxArchiveTaskAndCont(params,ajaxArchiveTaskAndContCallBack);
		}
	}

	/**
	 * 点击添加会员
	 */
	var onAddMember = function(){
		var params = {};
		params.queueId = $(this).data("queueId");
		params.className = "modal-addMember";
		$bb.modal({
			"tit":"添加会员",
			"className":params.className
		},function(){
			$ui.modalWrap({
				"dom":$("." + params.className + " .modal-body"),
				"flag":"1",
			});
			$servers.ajaxQryQueueCp(params,ajaxQryQueueCpCallBack);
		});
	}

	/**
     * 行绘制回调函数
     */
    var rowCallback = function(row, data, index) {
        // 操作
        var h2 = '<div class="operate-box"><i class="fa fa-search color-blue" data-toggle="tooltip" data-placement="top" title="查看详情"></i><i class="fa fa-trash-o color-red ml8" data-toggle="tooltip" data-placement="top" title="删除"></i></div>';
        $('td.operate', row).html(h2);
        $("td.operate .operate-box i.fa-search",row).unbind("click");
        $("td.operate .operate-box i.fa-search",row).data("data",data);
        $("td.operate .operate-box i.fa-search",row).click($event.onInfoSearch);
        $("td.operate .operate-box i.fa-trash-o",row).unbind("click");
        $("td.operate .operate-box i.fa-trash-o",row).data("data",data);
        $("td.operate .operate-box i.fa-trash-o",row).click($event.onDeleteUser);
    };

	/**
     * 获取datatable里的options
     */
    var getOptions = function(){
        var sessionId = $bb.getSessionStorage("sessionId");
        var options = {
            "ajax": {
                "url": URL + "ctrl/ptmanage/qryUserListTable?sessionId=" + sessionId,
                "data": $servers.inParams,
                "dataSrc": $servers.outParams
            },
            "columns": $servers.columnsData(),
            "rowCallback": rowCallback,
            "initComplete": $servers.initComplete,
        };
        return options;
    }

	classDetail.init = function(){
		var params = $bb.getPageParams();
		$servers.ajaxClassInfo(params.queueId,$event.ajaxClassInfoCallBack);
		// 添加会员
		$("#addMember").unbind("click");
		$("#addMember").data("queueId",params.queueId);
		$("#addMember").click(onAddMember);
		// datatable
        var id = $(".layout-bottom table").attr("id");
        $datatable.init({
            "id": id,
            "options": getOptions()
        });
	}

	return classDetail;
});